<?php
	include('include/header.php');
	include("include/commands.php");
        
        //Check if we are coming to this page from an insert query.
        if ($_POST["fromInsert"] == "true") {
            
            //Instantiate connector.
            $connector = new CloudDBInterface();
            
            //Set the car as sold.
            
            //Extract the id from the string.
            $car = $_POST["carSold"];
            $carId = strtok($car, ":");
            
            $query =    "UPDATE Inventory
                        SET Sold = \"Sold\"
                        WHERE id = $carId";
            
            //Mark the car as sold
            $connector->updateQuery($query);
            
            //Add the new Sale now.
            $commission = $_POST["commission"];
            $total = $_POST["total"];
            
            $employee = $_POST["employee"];
            $empId = strtok($employee,":");
            
            //Create the query.
            $query =    "INSERT INTO Transactions (InventoryId, Commission, Total, EmployeeId, Date)
                        VALUES ($carId, $commission, $total, $empId, CURDATE())";

            //Insert the tuple.
            $connector->insertQuery($query);            
        }
        
	if(isset($_GET['sortBy'])) {
		if(!isset($_GET['start']) || !isset($_GET['end'])) {
			$transactions = getTransactionsSorted($_GET['sortBy'], $_GET['id']);
		}
		else {
			$transactions = getTransactionsSorted($_GET['sortBy'], $_GET['id'], $_GET['start'], $_GET['end']);
		}
		
	}
	else {
		$transactions = getTransactions();
	}
?>

	<div class="body">
	
		<h1> Transaction History </h1>
		
		<div class="border">
			<form>
			<p>
			Date From <input type="text" name="start"/>
			Date To <input type="text" name="end"/>(yyyy-mm-dd)
			<br/>
			Employee ID: <input type="text" name="id"/>
			</br>		
			Sort By:
				<select name="sortBy">
					<option>NONE</option>
					<option>Price</option>
					<option>Date</option>
				</select>
			<input type="submit" value="Filter"/>
			</p>
			</form>
		</div>
                <br/>
                <div class="border">
                    <form id="newTransactionForm" action="sales.php" method="post" onsubmit="return validateSalesInsert();">
                        <h4>Add a New Sale</h4>
                        <p>
                            Car* <?php availableCars(); ?><br/>
                            Employee Name* <?php employeeNames(); ?><br/>
                            Commission* <input type="text" name="commission" id="commission"/><br/>
                            Transaction Total* <input type="text" name="total" id="total"/><br/>
                            
                            <input type="hidden" name="fromInsert" id="fromInsert" value="true"/>
                            <input type="submit" value="Submit"/>
                        </p>
                    </form>
                </div>
	</br>
		<?php 

			// ADD DATE SELECTION HERE

			if(sizeof($transactions) > 0){
					generateTransactionsTable($transactions);
				}
				else{
					echo "<p> No Transaction(s) Found. </p>";
				}
				
				function generateTransactionsTable($transactions) {
					echo "<table>";
					echo "<tr>";
					echo "<th>Id</th>";
					echo "<th>Model</th>";
					echo "<th>Color</th>";				
					echo "<th>Year</th>";
					echo "<th>Price($)</th>";
					echo "<th>Commission($)</th>";
					echo "<th>Total($)</th>";
					echo "<th>Employee Name</th>";
					
					echo "<th>Date</th>";
					echo "</tr>";
					generateTableBody($transactions);
					echo "</table>";
				}
		?>
	</div>


<?php 
	include('include/footer.php'); 
?>